#Programmer: Rory Carmichael
#Program: rc-min-max-dir.py
#Purpose: Weaver script to pipeline and distribute min-max for a directory of orfeomes
# and rrts
#Usage: rc-min-max-dir.py <directory>

import shlex
import subprocess
from weaver.nest import Nest
from weaver.function import PythonFunction, Function
from array import array
import re
import sys

def usage(): 
	print "Usage: weaver.py rc-min-max-dir.py <directory>\n"

if len(CurrentScript().arguments) != 1:
	usage()
	sys.exit(1)

originals = Glob(str(CurrentScript().arguments[0]+"/*.fasta"))

#Pile of functions
calc_codon_usage = Function('rc-get-codon-frequency.pl', cmd_format = "{executable} {inputs} > {outputs}")
min_max = Function('rc-min-max.pl', cmd_format = "{executable} {inputs} > {outputs}")
rm = Function('rm', cmd_format = '{executable} {inputs}')
make_cm = Function('CodonMultipliers.pl', cmd_format = "{executable} {inputs} {outputs}")
make_gc3 = Function('CodonPrintGC3.pl', cmd_format = "{executable} {inputs} {outputs}")
make_rrt = Function('RRTsReadCodonListGC3.pl', cmd_format = "{executable} {inputs} {outputs}")

for i in originals:
	cf = str(i) + ".cf"
	mm = str(i) + ".mm"
	cm_out = str(i) + ".multipliers"
	gc3_out = str(i) + ".gc3"
	rrt_out = str(i) + ".rrt"
	calc_codon_usage(inputs=i, outputs=cf, local=True)
	min_max( inputs=[i,cf], outputs=mm)
	make_cm(inputs=i,outputs=cm_out)
	make_gc3(inputs=[i,cm_out], outputs=gc3_out)
	make_rrt(inputs=[i,gc3_out], outputs=rrt_out)
